package airport.user.action;

import airport.user.pojo.User;
import airport.user.dao.UserDAO;
import airport.forum.dao.PrivateMessageDAO;

import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

//view profile
public class ViewProfileAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
	
	private static final long serialVersionUID = 1L;
	
	private String username = null;
	private String email = null;
	private String gender = null;
	private String age = null;
	private String occupation = null;
	private String travelReason = null;
	private String avatar = null;
	private User user = null;
	private int totalPMCount;
	private int unreadCount;
	
	
	protected HttpServletResponse servletResponse;  
	@Override   
	public void setServletResponse(HttpServletResponse servletResponse) {   
		this.servletResponse = servletResponse;   
	}   
	
	protected HttpServletRequest servletRequest;   
	@Override   
	public void setServletRequest(HttpServletRequest servletRequest) {   
		this.servletRequest = servletRequest;   
	} 
	
	public String execute() {
		//get the user through cookie
		username = user.getUserName();
		System.out.println(username);
		email = user.getEmail();
		gender = user.getGender();
		age = user.getAge();
		occupation = user.getOccupation();
		travelReason = user.getTravelReason();
		avatar = user.getAvatar();
		if (avatar == null) 
			avatar = "images/100x100.gif";
		
		//else 
			//avatar = "images/100x100.gif";
		System.out.println("avatar:"+avatar);
		PrivateMessageDAO pmDAO =  new PrivateMessageDAO();
		totalPMCount = pmDAO.getTotalMessagesCount(username);
		unreadCount = pmDAO.getUnreadMessagesCount(username);
		return "success";
	}
	
	public void validate() {
		//add validation/user exists in the DB
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		String username = null;
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					username = cookies[i].getValue();
					break;
				}
			}
		}
		if (username == null) {
			addFieldError("username","login has expired");
		}
		else {
			UserDAO dao = new UserDAO();
			user = dao.findUserByUserName(username);
			if (user == null) {
				addFieldError("username","invalid user");
			}
		}
	}


	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}
	
	public String getOccupation() {
		return occupation;
	}

	public void setOccupation(String occupation) {
		this.occupation = occupation;
	}

	public String getTravelReason() {
		return travelReason;
	}

	public void setTravelReason(String travelReason) {
		this.travelReason = travelReason;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getTotalPMCount() {
		return totalPMCount;
	}

	public void setTotalPMCount(int totalPMCount) {
		this.totalPMCount = totalPMCount;
	}

	public int getUnreadCount() {
		return unreadCount;
	}

	public void setUnreadCount(int unreadCount) {
		this.unreadCount = unreadCount;
	}


}
